package com.jeesite.modules.ch.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.http.HttpClientUtils;
import com.jeesite.modules.utils.AppCredentialGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ch.entity.ChChannel;
import com.jeesite.modules.ch.dao.ChChannelDao;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.client.RestTemplate;

/**
 * ch_channelService
 * @author 李寒冰
 * @version 2025-08-27
 */
@Service
@EnableTransactionManagement
public class ChChannelService extends CrudService<ChChannelDao, ChChannel> {

	@Value("${qudao.host}")
	private String qudaoHost;
	
	/**
	 * 获取单条数据
	 * @param chChannel
	 * @return
	 */
	@Override
	public ChChannel get(ChChannel chChannel) {
		return super.get(chChannel);
	}
	
	/**
	 * 查询分页数据
	 * @param chChannel 查询条件
	 * @param chChannel page 分页对象
	 * @return
	 */
	@Override
	public Page<ChChannel> findPage(ChChannel chChannel) {
		return super.findPage(chChannel);
	}

	public ChChannel getByUserCode(String  userCode) {
		ChChannel chChannel = new ChChannel();
		chChannel.setUserCode(userCode);
		return dao.getByEntity(chChannel);
	}
	
	/**
	 * 查询列表数据
	 * @param chChannel
	 * @return
	 */
	@Override
	public List<ChChannel> findList(ChChannel chChannel) {
		return super.findList(chChannel);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param chChannel
	 */
	@Override
	@Transactional
	public void save(ChChannel chChannel) throws RuntimeException {
		if(StringUtils.isEmpty(chChannel.getAppId())){
			ChChannel byAppid = null;
			String appid = null;
			do{
				appid = AppCredentialGenerator.generateAppId();
				byAppid = dao.findByAppid(appid);
			}while(byAppid!=null);
			chChannel.setAppId(AppCredentialGenerator.generateAppId());
		}
		if(StringUtils.isEmpty(chChannel.getAppSecret())){
			chChannel.setAppSecret(AppCredentialGenerator.generateAppSecret());
		}
		//同步到媒体后台
		Map<String,String> map = new HashMap<>();
		map.put("channelId",chChannel.getId());
		map.put("zhanghao",chChannel.getZhanghao());
		map.put("company",chChannel.getCompany());
		String post = HttpClientUtils.post(qudaoHost + "/admin/api/creatChannnelZhanghao", map);
		JSONObject jsonObject = JSONObject.parseObject(post);
		if(!jsonObject.getString("result").equals("true")){
			throw new RuntimeException(jsonObject.getString("message"));
		}else{
			if(StringUtils.isNotEmpty(jsonObject.getString("data"))){
				chChannel.setUserCode(jsonObject.getString("data"));
			}
		}
		super.save(chChannel);
	}
	
	/**
	 * 更新状态
	 * @param chChannel
	 */
	@Override
	@Transactional
	public void updateStatus(ChChannel chChannel) {
		super.updateStatus(chChannel);
	}
	
	/**
	 * 删除数据
	 * @param chChannel
	 */
	@Override
	@Transactional
	public void delete(ChChannel chChannel) {
		super.delete(chChannel);
	}
	
}